/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
 Mudanca: tirei depois de URLCon.... : 
                    //connection.addRequestProperty("Referer", HTTP_REFERER);
 */
package googleSearch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.json.JSONArray;      
import org.json.JSONObject;

public class GoogleQuery {

	// Put your website here
	//private final String HTTP_REFERER = "http://www.example.com/";
	
	public GoogleQuery() { }

	public String makeQuery(String query) {
	
            String results = new String();
            String aux = new String();
            results = results.concat("GOOGLE RESULTS ");

            try
            {
                for (int page=0; page < 8; page++){                    

                    // Convert spaces to +, etc. to make a valid URL
                    query = URLEncoder.encode(query, "UTF-8");
                    URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?start="+
                            page*8+"&rsz=large&v=1.0&q=" + query);
                    URLConnection connection = url.openConnection();

                    // Get the JSON response
                    String line;
                    StringBuilder builder = new StringBuilder();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()));
                    while((line = reader.readLine()) != null) {
                            builder.append(line);
                    }

                    String response = builder.toString();
                    JSONObject json = new JSONObject(response);

                    /*if (page == 0){                            
                            results = results.concat ("(about "+ json.getJSONObject("responseData")
                            .getJSONObject("cursor").getString("estimatedResultCount")) +
                                    " results):";
                    }*/

                    JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");

                    for (int i = 0; i < ja.length(); i++) {
                        results = results.concat("\n\n" + (1+i+8*(page)) + ". ");             
                        JSONObject j = ja.getJSONObject(i);results.concat("\n" + j.getString("titleNoFormatting")); 
                        results = results.concat(j.getString("titleNoFormatting"));                   
                        results = results.concat("\n" + j.getString("url"));           
                        results = results.concat("\n" + j.getString("content"));
                    }
                }
            }
		
            catch (Exception e) {
                    System.err.println("Something went wrong...");
                    e.printStackTrace();
            }
            
            return results;
	}


}